home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1546 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.4 KB  |  77 lines

  1. Newsgroups: comp.lang.c++
  2. Path: twisto.eng.hou.compaq.com!news
  3. From: Saurabh Dixit <saurabhd@bangate.compaq.com>
  4. Subject: IsA v/s HasA problem
  5. Message-ID: <30F52C66.851@bangate.compaq.com>
  6. Sender: news@twisto.eng.hou.compaq.com (System Administrator)
  7. Mime-Version: 1.0
  8. X-Mailer: Mozilla 2.0b2 (Windows; I; 32bit)
  9. Content-Type: text/plain; charset=us-ascii
  10. Organization: Compaq Computer Corporation
  11. Date: Thu, 11 Jan 1996 15:27:34 GMT
  12. X-Nntp-Posting-Host: 172.18.176.8
  13. Content-Transfer-Encoding: 7bit
  14.  
  15. Hi!
  16.  
  17. I always had no trouble distinguishing between the two...
  18.  
  19. But now, I for a specific case, I fail to see the repercussions of
  20. using the wrong relationship between objects.
  21.  
  22. This is my scenario.
  23. I have an object of type Base.  This is an abstract class.
  24. From Base I derive several different objects.
  25. I want to maintain a list of such objects for which I have a class
  26. Collection that encapsulates a typed array of Base objects.
  27.  
  28. I now want to define an object - let's call it Duh - that maintains a 
  29. collection of Base objects.  Duh objects are entities on their own and could
  30. have other attributes, BUT NEVER ANOTHER COLLECTION.
  31.  
  32. From what I gather, I am then leaning towards a HasA relationship, i.e.
  33. class Collection
  34. {
  35. public:
  36.     BOOL Add (Base* b);
  37. };
  38. class Duh 
  39. {
  40. private:
  41.     Collection c;
  42. };
  43.  
  44. Obviously if I was going to have 2 or more collections, then I would have had
  45. to have a HasA relationship.  But as said earlier, this is not the case!
  46. Also, I want to have some operations common between Collection and Duh.
  47. e.g. I can Add() a Base (derived!) object to a Collection and I also want
  48. to Add() an object to Duh.  In a HasA relationship I would simply 
  49. implemenent this as follows.
  50. BOOL Duh::Add (Base* b)
  51. {
  52.     // simply pass on to Collection method
  53.     return c.Add (b);
  54. }
  55. However for an IsA relationship, I would simply do
  56. class Duh : public Collection 
  57. {
  58. };
  59. and I am done.
  60.  
  61. Am I wasting my time too much thinking about this?  Regardless of code
  62. implementation, from the logical perspective, what is the correct object-
  63. oriented solution for my problem, HasA or IsA?
  64.  
  65. Any help will be greatly appreciated
  66. thanks
  67. saurabh
  68. -- 
  69. Saurabh Dixit
  70. "All opinions are always my own..."
  71. Compaq Computer Corporation
  72. ------------------------------------------------------------------------
  73. Thou who sneezes without kerchief takes matters in own hands.
  74.             - SomeoneIOnceNew
  75. ------------------------------------------------------------------------ 
  76. xxxx
  77.